package com.app.Activity;

import com.app.Controller.NetworkController;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import app.com.ModuleRole.R;

public class frmBieuQuyetActivity extends Activity{
	public static Button btnagree ;
	public static Button btndisagree;
	int port = 8800;
	public static int timeVote;
	private static int minutes;
	private int second;
	private Thread thread;
	private boolean isRunning;
	public static Runnable doEndTime;
	private AlertDialog.Builder _builder;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_bieu_quyet);
		
		/*Log.d("PEERDROID","time" +timeVote );
		minutes = timeVote / 60;
		second = timeVote - minutes * 60;
		runTimeVote();
		doEndTime=new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				doEndTime();
			}
		};*/
		
		NotificationManager mn=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
       
		
		Bundle ex = this.getIntent().getExtras();
		final int id = ex.getInt("ID");
		final int index = ex.getInt("INDEX");
		final String sender = ex.getString("SENDER");
		Log.d("PEERDROID","Da nhan duoc address Sender: "+ sender);
		Log.d("PEERDROID","Da nhan duoc index: "+ index);
		mn.cancel(id);
		
		btnagree = (Button)findViewById(R.id.btnAgree);
		btnagree.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int value = 1;
				NetworkController.sendResultBieuQuyet("192.168.1.77:"+port, index,value);
				frmBieuQuyetActivity.this.finish();
			}
		});
		
		btndisagree = (Button)findViewById(R.id.btnDisagree);
		btndisagree.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int value = 0;
				NetworkController.sendResultBieuQuyet("192.168.1.77:"+port, index,value);
				frmBieuQuyetActivity.this.finish();
			}
		});
		
	}
	
	/*public void runTimeVote(){
		isRunning = true;
		thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Log.d("PEERDROID","da vao run thread");
					Log.d("PEERDROID","time" +timeVote );
					Log.d("PEERDROID","_time_min" + minutes);
					Log.d("PEERDROID","_time sec: " + second);
					while(timeVote > 0 && isRunning) {
						if(second == 0)
						{
							second = 60;
							minutes--;
						}
						Thread.sleep(1000);
						handler.sendMessage(handler.obtainMessage());
					}
					if(isRunning)
						//ActivityController.DoEndTime();
						Log.d("PEERDROID","running");
						NetworkController.doEndTimeKick();
						Log.d("PEERDROID","do end time");
				}
				catch (Throwable t) {
				}
			}
		}) ;
		thread.start();
	}*/
	
	/*Handler handler=new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Log.d("PEERDROID","Vao hander");
			timeVote --;
			second--;
			
		}
	};*/
	
	/*private void doEndTime()
	{//vao do end time bi sai
		Log.d("PEERDROID","vao do end time");
		//set data current
		thread.stop();
		Log.d("PEERDROID","thread.stop();");
		_builder.setMessage("Đã hết thời gian biểu quyết!")
		.setCancelable(false)
		.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int id) {
				finish();
			}
		});
		AlertDialog alert = _builder.create();
		alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(0, 255, 255, 255))); 
		alert.show();
	}*/
}
